package com.fhx.core.base

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import androidx.fragment.app.DialogFragment
import java.lang.ref.WeakReference

/**
 * author fhx
 * create time 2020/4/29 9:40
 **/
abstract class BaseDialogFragment : DialogFragment() {
    protected var mContext: Context? = null
    private var mRootView: View? = null
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        mContext = WeakReference(activity).get()
        mRootView = LayoutInflater.from(mContext).inflate(getLayoutId(), null)
        val dialog = Dialog(mContext!!, getDialogStyle())
        dialog.setContentView(mRootView!!)
        initView(arguments)
        dialog.setCancelable(canCancel())
        dialog.setCanceledOnTouchOutside(canCancel())
        setWindowAttributes(dialog.window)
        return dialog
    }

    protected abstract fun getLayoutId(): Int

    protected abstract fun getDialogStyle(): Int
    protected abstract fun initView(arguments: Bundle?)
    protected abstract fun canCancel(): Boolean

    protected abstract fun setWindowAttributes(window: Window?)

    protected open fun findViewById(id: Int): View? {
        return if (mRootView != null) {
            mRootView!!.findViewById(id)
        } else null
    }
}